package com.example.sherlockzhong.plush_v2.Subscriptions;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Matrix;
import android.graphics.PointF;
import android.graphics.Rect;
import android.os.Bundle;
import android.util.DisplayMetrics;
import android.util.FloatMath;
import android.view.MotionEvent;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;

import com.example.sherlockzhong.plush_v2.Components.buttons.BackButton;
import com.example.sherlockzhong.plush_v2.R;
import com.umeng.analytics.MobclickAgent;

public class MapActivity extends Activity {

    private MapActivity self = this;

    private RelativeLayout rootContainer;
    private LinearLayout imgContainer;
    private ImageView imgBody;
    private Button streetViewBtn;
    private Bitmap bitmap;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.map);
        ((BackButton) findViewById(R.id.map_back)).setApp(this);
        saveComponents();

        imgBody.setOnTouchListener(new MultiTouchListener(imgBody));
        imgBody.setScaleType(ImageView.ScaleType.CENTER_INSIDE);
        bitmap = generateBitmap();

        if (bitmap != null) {
            imgBody.setImageBitmap(bitmap);
        }

        streetViewBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent streetViewActivity = new Intent(MapActivity.this, StreetViewActivity.class);
                startActivity(streetViewActivity);
                overridePendingTransition(android.R.anim.slide_in_left, android.R.anim.slide_out_right);
//                Toast.makeText(self.getBaseContext(), "街景功能正在抢修中。。。", Toast.LENGTH_SHORT).show();
            }
        });
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        recycleBitmap();
    }

    @Override
    public void onPause() {
        super.onPause();
        MobclickAgent.onPause(this);
    }

    @Override
    public void onResume() {
        super.onResume();
        MobclickAgent.onResume(this);
    }

    private void saveComponents() {
        rootContainer = (RelativeLayout) findViewById(R.id.map_root_container);
        imgContainer = (LinearLayout) findViewById(R.id.map_img_container);
        imgBody = (ImageView) findViewById(R.id.map_img_body);
        streetViewBtn = (Button) findViewById(R.id.street_view_btn);
    }

    private Bitmap generateBitmap() {
        Bitmap bitmapTmp = BitmapFactory.decodeResource(getResources(), R.drawable.map);
        return bitmapTmp;
    }

    private void recycleBitmap() {
        if (bitmap != null && !bitmap.isRecycled()) {
            bitmap.recycle();
            bitmap = null;
            System.gc();
        }
    }

    public class MultiTouchListener implements View.OnTouchListener {
        static final int NONE = 0;
        static final int DRAG = 1;
        static final int ZOOM = 2;
        public ImageView imageView;
        Matrix matrix = new Matrix();
        Matrix savedMatrix = new Matrix();
        int mode = 0;
        PointF start = new PointF();
        PointF middle = new PointF();
        float lastDistance = 1f;
        Rect rect;
        private int displayWidth;
        private float top;
        private float right;
        private float bottom;
        private float left;
        private float width;
        private float height;
        private float curWidth;

        public MultiTouchListener(ImageView iv) {
            super();
            DisplayMetrics dm = new DisplayMetrics();
            getWindowManager().getDefaultDisplay().getMetrics(dm);
            displayWidth = dm.widthPixels;
            curWidth = displayWidth;
            this.imageView = iv;
        }

        @Override
        public boolean onTouch(View view, MotionEvent motionEvent) {
            this.imageView.setScaleType(ImageView.ScaleType.MATRIX);
            ImageView iv = (ImageView) view;

            switch (motionEvent.getAction() & MotionEvent.ACTION_MASK) {
                case MotionEvent.ACTION_DOWN:
                    matrix.set(iv.getImageMatrix());
                    savedMatrix.set(matrix);
                    start.set(motionEvent.getX(), motionEvent.getY());
                    mode = DRAG;

                    float[] values = new float[9];
                    savedMatrix.getValues(values);
                    rect = iv.getDrawable().getBounds();
                    left = values[Matrix.MTRANS_X];
                    top = values[Matrix.MTRANS_Y];
                    width = rect.width() * values[Matrix.MSCALE_X];
                    height = rect.height() * values[Matrix.MSCALE_Y];
                    right = left + width;
                    bottom = top + height;
                    break;
                case MotionEvent.ACTION_POINTER_DOWN:
                    lastDistance = getDistance(motionEvent);
                    if (lastDistance > 10f) {
                        savedMatrix.set(matrix);
                        setMiddle(middle, motionEvent);
                        mode = ZOOM;
                    }
                    break;
                case MotionEvent.ACTION_UP:
                    break;
                case MotionEvent.ACTION_POINTER_UP:
                    mode = NONE;
                    break;
                case MotionEvent.ACTION_MOVE:
                    if (mode == DRAG) {
                        float dx = motionEvent.getX() - start.x;
                        float dy = motionEvent.getY() - start.y;
                        matrix.set(savedMatrix);
                        if (right - left < iv.getWidth()) {
                            dx = 0;
                        } else if (left + dx > 0 && dx > 0) {
                            dx = -left;
                        } else if (right + dx < iv.getWidth() && dx < 0) {
                            dx = iv.getRight() - right;
                        }
                        if (bottom - top < iv.getHeight()) {
                            dy = 0;
                        } else if (top + dy > 0 && dy > 0) {
                            dy = -top;
                        } else if (bottom + dy < iv.getBottom() && dy < 0) {
                            dy = iv.getBottom() - bottom;
                        }
                        matrix.postTranslate(dx, dy);
                    } else if (mode == ZOOM) {
                        float currentDistance = getDistance(motionEvent);
                        if (currentDistance > 10f) {
                            matrix.set(savedMatrix);
                            float scale = currentDistance / lastDistance;
                            if (width * scale <= 4000 && width * scale >= displayWidth) {
                                matrix.postScale(scale, scale, middle.x, middle.y);
                                curWidth = width * scale;
                            } else {
                                matrix.postScale(curWidth / width, curWidth / width, middle.x, middle.y);
                            }
                        }
                    }
                    break;
                default:
                    break;
            }

            iv.setImageMatrix(matrix);

            return true;
        }

        private float getDistance(MotionEvent motionEvent) {
            float x = motionEvent.getX(0) - motionEvent.getX(1);
            float y = motionEvent.getY(0) - motionEvent.getY(1);
            return FloatMath.sqrt(x * x + y * y);
        }

        private void setMiddle(PointF pointF, MotionEvent motionEvent) {
            float x = motionEvent.getX(0) + motionEvent.getX(1);
            float y = motionEvent.getY(0) + motionEvent.getY(1);
            pointF.set(x / 2, y / 2);
        }
    }
}
